import threading
import time

import blog_spider


def single_thread():
    # 单线程
    print("single-thread begin")
    for url in blog_spider.urls:
        blog_spider.craw(url)
    print("single-thread end")


def multi_thread():
    # 多线程
    print("multi-thread begin")
    # threads 存储线程对象
    threads = list()
    for url in blog_spider.urls:
        threads.append(
            # target 传递的是调用的函数, 当前调用的函数需要开启线程来处理, args为函数的参数, 需要传递一个元祖类型
            threading.Thread(target=blog_spider.craw, args=(url,))
        )
    for thread in threads:
        # 使用start()启动线程
        thread.start()
    # 使用join()函数等待结束
    for thread in threads:
        thread.join()
    print("multi-thread end")


if __name__ == "__main__":
    # 计算使用单线程爬取的耗时
    start = time.time()
    single_thread()
    end = time.time()
    print("single-thread cost: ", end - start, " seconds")

    # 计算使用多线程爬取的耗时
    start = time.time()
    multi_thread()
    end = time.time()
    print("multi-thread cost: ", end - start, " seconds")